<?php
defined('SYSPATH') or die('No direct script access.');

class Controller_Discussions extends Controller_Initialize {

  public $secure_actions = array(
      // user actions
      'add' => 'admin',
      'my' => 'admin',
      'edit' => 'admin',
      'edit_admin' => 'admin',
      'delete' => 'admin',
      'lock' => 'admin',
      'unlock' => 'admin',
      );

    public function action_index($url = null) {

      $this->template->navigation->active = 'discussions';
     
      $discussions = ORM::factory('discussion')->find_all();
      $this->template->content = View::factory('templates/discussions/index')
              ->bind('discussions', $discussions);
   
    }

    public function action_view($url) {

      $this->template->navigation->active = 'discussions';

      $id = explode("-", $url);
      $reply = ORM::factory('discussions_reply')->where('to_id','=',$id[0])->find_all();
      $discussion = ORM::factory('discussion', $id[0]);
      $this->template->content = View::factory('templates/discussions/view')
              ->bind('reply', $reply)
              ->bind('discussion', $discussion);

    }

    public function action_category($name) {

      $this->template->navigation->active = 'discussions';

      $discussions = ORM::factory('discussion')->where('category','=',$name)->find_all();
      $this->template->content = View::factory('templates/discussions/index')
              ->bind('discussions', $discussions);

    }

    public function action_add() {

      $this->template->navigation->active = 'discussions';
      $cat = ORM::factory('discussions_category')->find_all();

      $view = View::factory('templates/discussions/add')->bind('cat', $cat);

      if (!empty($_POST)) {

        $discussions = null;
        $discussions = ORM::factory('discussion');
        $post = $discussions->validate_add($_POST);
        $time = time();

        if ($post->check()) {

          $discussions->username = $this->userAuth->username;
          $discussions->title = $post['title'];
          $discussions->category = $post['category'];
          $discussions->seo_title = $post['seo_title'];
          $discussions->text = $post['text'];
          $discussions->date = $time;
          $discussions->save();

          if ($discussions->saved()) {
            Message::add('success', 'Téma bylo přidáno.');
            Request::instance()->redirect('discussions');
          }

        } else {

          $view->set('dataPost', $_POST);
          $informations = null;

          foreach($post->errors('discussions') as $item) {
            $informations .= '<span>'.$item.'</span>';
          }
          Message::add('warning', '<strong>Téma nebylo přidáno.</strong> '.$informations);
        }
      }

      $this->template->content = $view;

    }

    public function action_my() {

      $this->template->navigation->active = 'discussions';

      $this->template->content = View::factory('templates/discussions/my')->bind('discussions', $discussions);

      $discussions = ORM::factory('discussion')->where('username', '=', $this->userAuth->username)->find_all();

    }

    public function action_edit($id) {

      $this->template->navigation->active = 'discussions';

      $this->template->content = View::factory('templates/discussions/edit')
              ->bind('cat', $cat)
              ->bind('discussion', $discussion);

      $discussion = ORM::factory('discussion')->where('username', '=', $this->userAuth->username)->find($id);
      $cat = ORM::factory('discussions_category')->find_all();

    }
    
    public function action_edit_admin($id) {
      
      $this->template->navigation->active = 'discussions';

      $this->template->content = View::factory('templates/discussions/edit_admin')
              ->bind('discussion', $discussion)
              ->bind('cat', $cat);

      $discussion = ORM::factory('discussion')->find($id);
      $cat = ORM::factory('discussions_category')->find_all();

    }

    public function action_edit_add($id) {

      $this->template->navigation->active = 'discussions';

      $view = View::factory('templates/discussions/edit');

      if (!empty($_POST)) {

        $discussions = null;
        if (Auth::instance()->logged_in('admin')) {
          $discussions = ORM::factory('discussion', $id);
        } else {
          $discussions = ORM::factory('discussion', $id)->where('username', '=', $this->userAuth->username);
        }
        $view->set('discussions', $discussions);
        $post = $discussions->validate_add_edit($_POST);

        if ($post->check()) {

          $discussions->title = $post['title'];
          $discussions->category = $post['category'];
          if (Auth::instance()->logged_in('admin')) {
            $discussions->seo_title = $_POST['seo_title'];
          }
          $discussions->text = $post['text'];
          $discussions->save();

          if ($discussions->saved()) {
            Message::add('success', 'Téma bylo upraveno.');
            if (Auth::instance()->logged_in('admin')) {
              Request::instance()->redirect('administration/discussions');
            } else {
              Request::instance()->redirect('discussions/my');
            }
          }

        } else {

          $view->set('dataPost', $_POST);
          $informations = null;

          foreach($post->errors('discussions') as $item) {
            $informations .= '<span>'.$item.'</span>';
          }
          Message::add('warning', '<strong>Téma nebylo upraveno.</strong> '.$informations);

          if (empty($discussions->locked)) {
            Message::add('warning', '<strong>Téma je uzamčeno a nelze ho již upravovat.</strong> '.$informations);
          }
        }
      }

      $this->template->content = $view;

    }

    public function action_delete($id) {

      if (Auth::instance()->logged_in('admin')) {
        if (ORM::factory('discussion')->delete($id)) {
          Message::add('success', 'Téma bylo úspěšně vymazáno.');
          Request::instance()->redirect('administration/discussions');
        }
      } else {
        if (ORM::factory('discussion')->where('username', '=', $this->userAuth->username)->delete($id)) {
          Message::add('success', 'Téma bylo úspěšně vymazáno.');
        }
        Request::instance()->redirect('discussions/my');
      }

    }

    public function action_lock($id) {

      $discussions = ORM::factory('discussion', $id);
      $discussions->locked = 1;
      $discussions->save();
      if ($discussions->saved()) {
        Message::add('success', 'Téma bylo úspěšně uzamčeno.');
        Request::instance()->redirect('administration/discussions');
      } else {
        Message::add('warning', 'Téma nebylo uzamčeno.');
        Request::instance()->redirect('administration/discussions');
      }

    }

    public function action_unlock($id) {

      $discussions = ORM::factory('discussion', $id);
      $discussions->locked = null;
      $discussions->save();
      if ($discussions->saved()) {
        Message::add('success', 'Téma bylo úspěšně odemčeno.');
        Request::instance()->redirect('administration/discussions');
      } else {
        Message::add('warning', 'Téma je stále uzamčeno.');
        Request::instance()->redirect('administration/discussions');
      }

    }

    public function after() {

      if ($this->auto_render) {
        $scripts = array('media/js/tinymce/tiny_mce.js');
        $this->template->scripts = array_reverse(array_merge($this->template->scripts, $scripts));
      }

      // Run anything that needs to run after this.
      parent::after();
    }
 } // End